export default {
  data() {
    return {
      mobile: '',
      pageIndex: 1,
      pageSize: 100,
      list: [],
      clicktrue: false
    }
  },
  onLoad(e) {
    //电话号码中间4位变成**
    this.mobile = e.phone
    this.LoginChooseStore()
  },
  //监听默认返回事件
  onBackPress(e) {
	  // if (this.type === 'new') {
	    uni.reLaunch({
	      url: '/pages_login/login/login',
	    })
	    return true
	  // }
  },
  methods: {
    // 获取列表
    async LoginChooseStore() {
      try {
        var card = this.mobile.replace(/\s/g, '') //正则去掉字符串中所有空格
        let data = {
          'pageIndex': this.pageIndex,
          'pageSize': this.pageSize,
          'mobile': card
        }
        const res = await this.$api.user.LoginChooseStore(data)
        console.log(res)
        this.list = res.data.list

      } catch (e) {
        console.log(e)
        //TODO handle the exception
      }
    },
    returnExname(data, role) {
      // 0、待审核 1、审核通过 2、审核拒绝
      let status = ''
      if (role === 2) {
        status = '进入公司'
      } else {
        if (data === 0) {
          status = '待审核'
        } else if (data === 1) {
          status = '进入公司'
        } else if (data === 2) {
          status = '审核拒绝'
        }
      }
      return status
    },
    // 创建企业
    async CreateEnterprise() {
      try {
        var card = this.mobile.replace(/\s/g, '') //正则去掉字符串中所有空格
        let data = {
          'Mobile': card
        }
        const res = await this.$api.company.CreatCompanyAgainAsync(data)
        let ispsd=this.list[0].isPWD?1:0
        uni.navigateTo({
				  url: '/pages_login/register/register?isPWD=' + ispsd + '&phone=' + card,
        })
      } catch (e) {
        let ispsd=this.list[0].isPWD?1:0
        uni.navigateTo({
          url: '/pages_login/register/register?isPWD=' + ispsd + '&phone=' + card,
        })
      }

    },

    // 进入公司
    async EnterCompany(index) {
			 this.clicktrue=true
			 uni.showLoading({
			 	title: '加载中'
			 })
      // if(!this.clicktrue){
      // 角色(0、员工 1、管理员 2、超管)
      let role = ''
      if (this.list[index].role === 0) {
        role = '员工'
      } else if (this.list[index].role === 1) {
        role = '管理员'
      } else if (this.list[index].role === 2) {
        role = '超级管理员'
      }
      console.log(typeof this.list[index].role,role)
      let store = {
        companyName: this.list[index].companyname,
        storeId: this.list[index].store_id,
        storeName: this.list[index].store_name,
        isPay: this.list[index].deposit_balance > 0 ? true : false
      }
      let user = {
        headImg: this.list[index].headportrait,
        jobName: this.list[index].job,
        roleName: role,
        userName: this.list[index].truename
      }
      try {
        const res = await this.$api.store.SwitchStoreInfo({
          Storeid: this.list[index].store_id,
        })
        // 储存用户登录基本信息
        uni.setStorageSync('Ztc_B_USER_INFO', res.data)
        uni.setStorageSync('token', res.data.token)
      } catch (err) {
        console.log(err)
      }
      let userInfo = this.$store.state.userInfoDetail
      userInfo.store = store
      user.isPWD=userInfo.user.isPWD
      user.login=userInfo.user.login
      userInfo.user = user
      uni.setStorageSync('userInfoDetail', userInfo)
      this.$store.commit('setUserInfoDetail', userInfo)

      let userMsg = this.$store.state.user.userInfo
      userMsg.tid = this.list[index].accountid
      userMsg.permious = this.list[index].permious
      userMsg.merchant_type=this.list[index].merchant_type
      userMsg.examine_status=this.list[index].examine_status
      userMsg.role = this.list[index].role
      this.$store.commit('user/setUserInfo', userMsg)
      this.$store.commit('setUserInfo', userMsg)
      //用户信息存储
      uni.setStorage({
        key: 'Ztc_B_USER_INFO',
        data: userMsg,
      })

      console.log(this.$store.state)
      uni.reLaunch({
        url: '/pages/index/index',
      })
      uni.hideLoading()

      // }
    }
  }
}
